package com.education.user.service.impl;

import com.education.user.mapper.LoginAttemptsDao;
import com.education.user.pojo.LoginAttempts;
import com.education.user.service.LoginAttemptsService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 登陆限制表(LoginAttempts)表服务实现类
 *
 * @author makejava
 * @since 2023-12-01 11:07:19
 */
@Service("loginAttemptsService")
public class LoginAttemptsServiceImpl implements LoginAttemptsService {
    @Resource
    private LoginAttemptsDao loginAttemptsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LoginAttempts queryById(Integer id) {
        return this.loginAttemptsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param loginAttempts 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<LoginAttempts> queryByPage(LoginAttempts loginAttempts, PageRequest pageRequest) {
        long total = this.loginAttemptsDao.count(loginAttempts);
        return new PageImpl<>(this.loginAttemptsDao.queryAllByLimit(loginAttempts, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param loginAttempts 实例对象
     * @return 实例对象
     */
    @Override
    public LoginAttempts insert(LoginAttempts loginAttempts) {
        this.loginAttemptsDao.insert(loginAttempts);
        return loginAttempts;
    }

    /**
     * 修改数据
     *
     * @param loginAttempts 实例对象
     * @return 实例对象
     */
    @Override
    public LoginAttempts update(LoginAttempts loginAttempts) {
        this.loginAttemptsDao.update(loginAttempts);
        return this.queryById(loginAttempts.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.loginAttemptsDao.deleteById(id) > 0;
    }

    @Override
    public LoginAttempts queryByPhone(String username) {
        return loginAttemptsDao.queryByPhone(username);
    }
}
